<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="标识" prop="tag">
        <el-input
          v-model="queryParams.tag"
          placeholder="请输入标识"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="类型" prop="type">
        <el-select
          v-model="queryParams.type"
          placeholder="请选择类型"
          clearable
        >
          <el-option
            v-for="dict in dict.type.flow_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="是否启用" prop="isActive">
        <el-select
          v-model="queryParams.isActive"
          placeholder="请选择类型"
          clearable
        >
          <el-option
            v-for="dict in dict.type.start_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['dynamic:workflow:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['dynamic:workflow:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['dynamic:workflow:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['dynamic:workflow:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="dynamicList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="编号" align="center" prop="mdCode" />
      <el-table-column label="名称" align="center" prop="mdName" />
      <el-table-column label="所属模块" align="center" prop="mdModel" />
      <el-table-column label="类型" align="center" prop="mdType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.flow_type" :value="scope.row.mdType" />
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['dynamic:workflow:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit-outline"
            @click="handleDesign(scope.row)"
            v-hasPermi="['dynamic:workfow:detail']"
            >条目</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['dynamic:workflow:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改工作流程管理对话框 -->
    <dynamic-dialog
      :is-show="dialogOpt.show"
      :type="dialogOpt.type"
      :dynamic-id="dialogOpt.dynamicId"
      @close="changeDialogOpt(false)"
      @updateList="getList"
    ></dynamic-dialog>
  </div>
</template>

<script>
import { getDynamicList, delDynamic } from "@/api/flow/dynamic.js";
import DynamicDialog from "./Dialog.vue";
import { DIALOG_TYPE } from "@/utils/const.js";

export default {
  name: "DynamicList",
  components: {
    DynamicDialog,
  },
  dicts: ["flow_type", "start_type"],
  data() {
    return {
      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 工作流程管理表格数据
      dynamicList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        tag: null,
        type: null,
        isActive: null,
        flieAddress: null,
        versions: null,
        deployBy: null,
      },
      dialogOpt: {
        dynamicId: null,
        show: false,
        type: DIALOG_TYPE.ADD,
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询动态表单列表 */
    async getList() {
      this.loading = true;
      try {
        const res = await getDynamicList(this.queryParams);
        if (res.code == 200) {
          this.dynamicList = res.rows;
        } else {
          this.dynamicList = [];
          this.$modal.msgError(`数据获取异常({res.code}): ${res.msg}`);
        }
      } catch (e) {
        this.$modal.msgError("列表获取异常");
      } finally {
        this.loading = false;
      }
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.loadView.pageNum = 1;
      this.getList();
    },
    changeDialogOpt(opt) {
      this.dialogOpt.show = opt.show || false;
      this.dialogOpt.type = opt.type || DIALOG_TYPE.ADD;
      this.dialogOpt.dynamicId = opt.dynamicId || null;
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.mdId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.dialogOpt.show = true;
      this.dialogOpt.type = DIALOG_TYPE.ADD;
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      // const dynamicId = row.mdId || this.ids;
      const dynamicId = row.mdId

      this.changeDialogOpt({
        dynamicId,
        show: true,
        type: DIALOG_TYPE.UPDATE,
      });
    },
    handleDesign(row) {
      this.$router.push({
        path: "/tool/build",
        params: { dynamicInfo: row },
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.mdId || this.ids;
      this.$modal
        .confirm('是否确认删除动态表单管理编号为"' + ids + '"的数据项？')
        .then(function () {
          return delDynamic(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "md/work/export",
        {
          ...this.queryParams,
        },
        `work_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
